# -*- coding: utf-8 -*-

import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt

sampleNo = 1000  # 数据数量
mu = 3
# 二维正态分布
mu = np.array([[1, 5]])
Sigma = np.array([[1, 0.5], [1.5, 3]])
R = cholesky(Sigma)
srcdata = np.dot(np.random.randn(sampleNo, 2), R) + mu
X = srcdata
plt.plot(srcdata[:, 0], srcdata[:, 1], 'bo')

from sklearn.cluster import KMeans

# Fitting K-Means to the dataset
kmeans = KMeans(n_clusters=2, init='k-means++', random_state=42)
y_kmeans = kmeans.fit_predict(X)

L1 = []
L2 = []
for i in range(len(y_kmeans)):
    L1.append(srcdata[i][0])
    L2.append(srcdata[i][1])

# 用来正常显示中文标签
plt.rc('font', family='SimHei', size=6)

# 用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False

p1 = plt.subplot(221)
plt.title(u"Kmeans聚类 n=2")
plt.scatter(L1, L2, c=y_kmeans, marker="s")
plt.sca(p1)

